package com.test.cordova.mvp.presenter;

import com.test.cordova.mvp.base.BasePresenter;
import com.test.cordova.mvp.base.net.BaseDisposableObserver;
import com.test.cordova.mvp.contract.MainContract;

import java.util.HashMap;

import io.reactivex.ObservableSource;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Administrator on 2019/2/22 0022.
 */

public class MainPresenter extends BasePresenter<MainContract.MainModel, MainContract.MainView> implements MainContract.MainPresenter {


    @Override
    public void getData() {//这里要注意判空（view和model可能为空）

        model.getDataFromNet()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseDisposableObserver<String>() {
                    @Override
                    public void responseResult(String result) {
                        getView().setTestTip(result);
                    }

                    @Override
                    public void errorResult(Throwable e, String errorMsg) {

                    }
                });

        //测试map 转换处理请求
        model.getDataFromNet()
                .map(new Function<String, HashMap<String,String>>() {
                    @Override
                    public HashMap<String,String> apply(String s) throws Exception {
                        HashMap<String,String> map = new HashMap<>();
                        map.put("key",s);
                        return map;
                    }
                }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<HashMap<String,String>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        d.isDisposed();
                        d.dispose();
                    }

                    @Override
                    public void onNext(HashMap<String,String> data) {
                        String key = data.get("key");
                        getView().setTestTip(key);
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });

        //测试嵌套请求  flatMap
        model.getDataFromNet()
                .flatMap(new Function<String, ObservableSource<String>>() {
                    @Override
                    public ObservableSource<String> apply(String s) throws Exception {
                        return model.getDataFromNet();
                    }
                }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(String s) {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });




    }

    @Override
    public void onViewDestroy() {

    }
}
